package com.wms.controller;

import com.wms.bean.ResultBean;
import com.wms.core.R;
import com.wms.dto.PermissionVo;
import com.wms.dto.SuserUpVo;
import com.wms.dto.SuserWareDTO;
import com.wms.entity.Suser;
import com.wms.entity.WarehouseNode;
import com.wms.service.SuserService;
import com.wms.service.WarehouseNodeService;
import com.wms.service.impl.WarehouseNodeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Api(tags = "库管理员权限管理--张开心")
@Slf4j
public class PermissionController {

    @Autowired
    private WarehouseNodeServiceImpl warehouseNodeService;


    @GetMapping("/query")
    @ApiOperation(value = "查询管理员列表")
    public ResultBean<List<WarehouseNode>> query(){
        log.info("初始查询库管理员列表");
        List<WarehouseNode> list = warehouseNodeService.selectTopLevelWarehouses();
        return list.isEmpty()? R.fail("查询失败"):R.success("查询成功", list);
    }

    @PutMapping("/updatePassword/{id}")
    @ApiOperation(value = "更改权限(更新状态)")
    public ResultBean getUserInfo(
            @ApiParam(value = "更改用户传入的信息", required = true)   @RequestBody PermissionVo permissionVo
    ) {
        int result= warehouseNodeService.updatePermission(permissionVo);
        return result==1?R.success("更改成功"):R.fail("更改失败");
    }


    @PostMapping("/insert")
    @ApiOperation(value = "新增库管员权限")
    public ResultBean<Integer> insert(
            @ApiParam(value = "库管员更新信息", required = true) @RequestBody PermissionVo permissionVo) {
        log.info("新增库管员: {}",permissionVo);
        int rows = warehouseNodeService.insertPermission(permissionVo);
        return rows==1?R.success("新增成功", rows):R.fail("新增失败");
    }

    @GetMapping("/pass/{id}")
    @ApiOperation(value = "根据用户id查询对应管理仓库")
    public ResultBean queryPermission(
            @ApiParam(value = "查询用户id", required = true)@PathVariable Integer id
    ) {
        List<SuserWareDTO> list = warehouseNodeService.selectUser(id);
        return list.size()!=0?R.success("查询成功",list):R.fail("查询失败");
    }

}
